﻿using System;
using System.Security.Cryptography;

namespace Framework.Crypto.Hashers
{
    /// <summary>
    /// HmacMd5 hash algorythm.
    /// </summary>
    public class HmacMd5Hasher : IHasher
    {
        /// <summary>
        /// HmacMD5 Crypto loaded
        /// </summary>
        private HMACMD5 hmacMD5M;

        /// <summary>
        /// Generic HmacMD5 constructor with some byte array to sign and a key.
        /// </summary>
        /// <param name="signer"></param>
        /// <param name="key"></param>
        public HmacMd5Hasher(byte[] signer, byte[] key)
            : base(signer, key)
        {
            hmacMD5M = new HMACMD5(Key);
        }

        /// <summary>
        /// Generic HmacMD5 constructor with some byte array to sign and a key length to generate.
        /// </summary>
        /// <param name="signer"></param>
        /// <param name="keyLength"></param>
        public HmacMd5Hasher(byte[] signer, int keyLength)
            : base(signer, keyLength)
        {
            hmacMD5M = new HMACMD5(key);
        }

        /// <summary>
        /// Returns computed by HmacMD5 hash byte array.
        /// </summary>
        /// <returns></returns>
        public override byte[] Sign()
        {
            return hmacMD5M.ComputeHash(signer);
        }

        /// <summary>
        /// Returns a string-based hash representation.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return BitConverter.ToString(Sign()).Replace("-", "");
        }

        /// <summary>
        /// Clears byte buffer.
        /// </summary>
        public override void Clear()
        {
            signer = new byte[0];
            hmacMD5M.Clear();
        }
    }
}
